﻿package com.sz.message.dao.impl;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.rmi.RemoteException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

import javax.xml.rpc.ServiceException;

import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.sz.message.dao.IMessageDAO;
import com.sz.common.ConstDefine;
import com.sz.common.ICommonDAO;
import com.sz.common.SZUtility;
import com.tempuri.WmgwLocator;

public class MessageDAO extends SqlMapClientDaoSupport implements IMessageDAO {
	
	private ICommonDAO commonDAO = null;

	public ICommonDAO getCommonDAO() {
		return commonDAO;
	}

	public void setCommonDAO(ICommonDAO commonDAO) {
		this.commonDAO = commonDAO;
	}
	
	@SuppressWarnings("unchecked")
	public long addMessage(Map map) throws Exception {
		Object obj = getSqlMapClient().insert("message_sqlmap.insert_message", map);
		return obj==null ? 0 : Long.parseLong(obj.toString());
	}
	
	@SuppressWarnings("unchecked")
	public long sendMessage(String phoneNum, String msg) throws Exception {
		Map map = new HashMap();
		
		String[] phoneNumList = phoneNum.split(",");
	
		for (int j = 0; j < phoneNumList.length; j++)
		{
			WmgwLocator wmgwLocator = new WmgwLocator();
			String strArgs[] = new String[10];
			
			//strArgs[0] = "j00590";第一次测试
			//strArgs[1] = "590690";第一次测试
			
			//strArgs[0] = "JC2110";第二次测试
			//strArgs[1] = "178036";第二次测试
			
			
			strArgs[0] = "J21632";//正式
			strArgs[1] = "266897";//正式
			
			//strArgs[2] = phoneNum;
			strArgs[2] = phoneNumList[j].trim();
			strArgs[3] = msg;
			strArgs[4] = "1";
			strArgs[5] = "*";
			//String strMsg = new String(strArgs[3].getBytes("UTF-8"));
			String strMsg = new String(strArgs[3]);
			
			
			
			map.put("PHONENUM", strArgs[2]);
			map.put("MSG", strMsg);
			//mongateCsSendSmsEx
			try {
				System.out.println("Test mongateCsSendSmsEx ...");
				String strCsSendSmsEx = wmgwLocator.getwmgwSoap().mongateCsSendSmsEx(strArgs[0],
						strArgs[1], strArgs[2], strMsg, Integer.valueOf(strArgs[4]).intValue());
				System.out.println("back value is :" + strCsSendSmsEx);
				System.out.println("send mongateCsSendSmsEx end !");
				System.out.println();
				
				map.put("CSSENDSMSEX", strCsSendSmsEx);
				
			} catch (RemoteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ServiceException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			//mongateCsSendSmsExNew
			try {
				System.out.println("Test mongateCsSendSmsExNew ...");
				String strCsSendSmsExNew = wmgwLocator.getwmgwSoap().mongateCsSpSendSmsNew(strArgs[0],
						strArgs[1], strArgs[2], strMsg, Integer.valueOf(strArgs[4]).intValue(),strArgs[5]);
				System.out.println("back value is :" + strCsSendSmsExNew);
				System.out.println("send mongateCsSendSmsExNew end !");
				System.out.println();
				
				map.put("CSSENDSMSEXNEW", strCsSendSmsExNew);
				
			} catch (RemoteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ServiceException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			//mongateCsGetStatusReportExEx
			try {
				System.out.println("Test mongateCsGetStatusReportExEx ...");
				String[] strRet = wmgwLocator.getwmgwSoap().mongateCsGetStatusReportExEx(strArgs[0], strArgs[1]);
				System.out.println("back value is :");
				if (strRet != null)
				{
					String strRetList = "";
					
					for(int i = 0; i < strRet.length; ++i)
					{
						if (i == 0)
						{
							strRetList += strRet[i];
						}
						else
						{
							strRetList += "|" + strRet[i];
						}
						
						System.out.println(strRet[i]);
					}
					map.put("CSGETSTATUSREPORTEXEX", strRetList);
				}
				else
				{
					System.out.println("null");
					map.put("CSGETSTATUSREPORTEXEX", "null");
				}
				System.out.println("send mongateCsGetStatusReportExEx end !");
				System.out.println();
			} catch (RemoteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ServiceException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	
			//mongateQueryBalance
			try {
				System.out.println("Test mongateQueryBalance ...");
				
				String strQueryBalance = "" + wmgwLocator.getwmgwSoap().mongateQueryBalance(strArgs[0], strArgs[1]);
				System.out.println("back value is :" + strQueryBalance);
				System.out.println("send mongateQueryBalance end !");
				System.out.println();
				
				map.put("QUERYBALANCE", strQueryBalance);
			} catch (RemoteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ServiceException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			//mongateCsGetSmsExEx
			try {
				System.out.println("Test mongateCsGetSmsExEx ...");
				String[] strRet = wmgwLocator.getwmgwSoap().mongateCsGetSmsExEx(strArgs[0], strArgs[1]);
				System.out.println("back value is :");
				if (strRet != null)
				{
					String strRetList = "";
					
					for(int i = 0; i < strRet.length; ++i)
					{
						if (i == 0)
						{
							strRetList += strRet[i];
						}
						else
						{
							strRetList += "|" + strRet[i];
						}
						
						System.out.println(strRet[i]);
					}
					
					map.put("CSGETSMSEXEX", strRetList);
				}
				else
				{
					System.out.println("null");
					
					map.put("CSGETSMSEXEX", "null");
				}
				System.out.println("send mongateCsGetSmsExEx end !");
				System.out.println();
			} catch (RemoteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ServiceException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	
			
			
			addMessage(map);
		}
		
		return 1;
	}
	
	/**
	 * 和订单有关的短信
	 */	
	@SuppressWarnings("unchecked")
	public String getSMS_Ticket(String ticket_id, String type) throws Exception {
		Map tmp = new HashMap();
		tmp.put("TICKET_ID", ticket_id);
		Map map = (Map)getSqlMapClient().queryForObject("ticket_sqlmap.query_Ticket", tmp);
		String cust_name = SZUtility.toString(map.get("CUST_NAME"));
		String b_name = SZUtility.toString(map.get("BUSINESS_NAME"));
		String b_address = SZUtility.toString(map.get("BUSINESS_ADDRESS"));
		String business_response = SZUtility.toString(map.get("BUSINESS_RESPONSE"));
		String discount_descrip = SZUtility.toString(map.get("DISCOUNT_DESCRIP"));
		
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		String demand_time_str = format.format(map.get("DEMAND_TIME"));
		
		
		StringBuffer buf = new StringBuffer();
		if(type.equals("1")) { //预订成功
			buf.append("尊敬的" + cust_name + ":");
			buf.append("您已成功预订" + b_name);
			if(business_response.length() > 0) {
				buf.append(business_response + "包间");
			}
			buf.append("。就餐时间：" + demand_time_str + "。");
			if(discount_descrip.length() > 0) {
				buf.append(discount_descrip + "。");
			}
			buf.append("祝您用餐愉快!");
		} else if(type.equals("2")) { //请柬短信
			buf.append(cust_name + "于");
			buf.append(demand_time_str + "在");
			if(b_address.length() > 0) {
				buf.append(b_address);
			}
			buf.append("，" + b_name);
			if(business_response.length() > 0) {
				buf.append(business_response + "包间");
			}
			buf.append("，恭候您的光临。");
			buf.append("如需询问，请致电" + ConstDefine.s_service_number);			
		} else if(type.equals("3")) { //预订失败
			buf.append("尊敬的" + cust_name + ":");
			buf.append("由于商家的原因，您预订的" + demand_time_str + b_name);
			buf.append("未成功，欢迎拨打" + ConstDefine.s_service_number);
			buf.append("再次预订!");			
		} else if(type.equals("4")) { //就餐提醒
			buf.append("尊敬的" + cust_name + ":");
			buf.append("您已成功预订" + b_name);	
			buf.append("。地址：" + b_address);
			buf.append("。就餐时间：" + demand_time_str + "。");			
			buf.append("请准时赴约，如有变动，请致电" + ConstDefine.s_service_number);
		}
		
		return buf.toString();
	}
	
	/**
	 * 商家短信（如商家优惠）
	 * @param business_id
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public String getSMS_Business(String business_id, String type) throws Exception {	
		
		Map tmp = new HashMap();
		tmp.put("BUSINESS_ID", business_id);
		Map map = (Map)getSqlMapClient().queryForObject("business_sqlmap.query_Business", tmp);
		String name = SZUtility.toString(map.get("NAME"));
		String address = SZUtility.toString(map.get("ADDRESS"));
		String discount = SZUtility.toString(map.get("DISCOUNT"));
		String discount_desc = SZUtility.toString(map.get("DISCOUNT_DESC"));
		
		StringBuffer buf = new StringBuffer();
		if(type.equals("1")) { //商家优惠
			buf.append(name);
			if(address.length() > 0) {
				buf.append("，" + address);
			}
			buf.append("。");
			
			if(discount.length() > 0) {
				long d = Long.parseLong(discount);
				if(d < 100) {
					d = d/10;
					buf.append("凭此短信享受" + d + "折优惠。");
				}
			}
			if(discount_desc.length() > 0) {
				buf.append(discount_desc + "。");
			}
			
			buf.append("详询" + ConstDefine.s_service_number);
		}
		return buf.toString();
	}
}
